package com.example.demo.Controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Service.EmployeeService;
import com.example.demo.Service.HRService;
import com.example.demo.Service.TokenService;


/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: RegisterController
 * @packageName: com.example.demo.Controller
 * @description: 这是用户进行注册的控制类
 * @data: 2021-07-14
 **/


@RequestMapping("/Register")
@RestController
public class RegisterController {

	@Autowired
	private HRService hrService;
	@Autowired
	private EmployeeService empService;
	@Autowired
	private TokenService tokenService;
 
	
	/**
	* @author:郑皓杰
	* @methodsName: registerhr
	* @description: hr在数据库进行注册
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/RegisterHR")
	public APIResponse<?> registerhr(HttpServletRequest httpServletRequest){
		String username=httpServletRequest.getParameter("username");
		
		String password=httpServletRequest.getParameter("password");
		//这里应该抛出一个异常，因为输入可能不是int
		Integer companyid=Integer.parseInt(httpServletRequest.getParameter("companyid"));
		return hrService.register(username,password,companyid);
	}
	
	
	/**
	* @author:郑皓杰
	* @methodsName: registeremp
	* @description: hr将未注册的employee在数据库进行注册
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/RegisterEmp")
	public APIResponse<?> registeremp(HttpServletRequest httpServletRequest) {
		//如果username telephone有一个为空 抛出异常；
		//如果birth recruited有一个为空 抛出异常；
		String realname=httpServletRequest.getParameter("realname");
		String gender=httpServletRequest.getParameter("gender");
		String birth=httpServletRequest.getParameter("birth");
		String email=httpServletRequest.getParameter("email");
		String telephone=httpServletRequest.getParameter("telephone");
		String position = httpServletRequest.getParameter("position");
		String recruited = httpServletRequest.getParameter("recruited");
		Integer salary = Integer.parseInt(httpServletRequest.getParameter("salary"));
		//Integer companyID = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		//String companyName = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyName();
		String companyName = tokenService.getCompanyNamefromToken(token);
		return hrService.addEmployeeToCompany(realname, gender, birth, email, telephone, position, recruited, salary, companyID, companyName);
	}
	
	
	/**
	* @author:郑皓杰
	* @methodsName: registerempself
	* @description: 未注册的employee自己在数据库进行注册
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/RegisterEmpSelf")
	public APIResponse<?> registerempself(HttpServletRequest httpServletRequest) {
		//如果传入输入为空，抛出异常
		String username=httpServletRequest.getParameter("username");
		String password=httpServletRequest.getParameter("password");
		String telephone=httpServletRequest.getParameter("telephone");
		return empService.register(username, telephone, password);
	}
}

